package com.suitsoft.ut.web.middle.dabian;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;


import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.google.common.collect.Lists;
import com.suitsoft.ut.entity.base.Student;
import com.suitsoft.ut.entity.commons.UploadFileInfo;
import com.suitsoft.ut.entity.thesis.Thesis;
import com.suitsoft.ut.entity.thesis.ThesisFile;
import com.suitsoft.ut.service.base.StudentManager;
import com.suitsoft.ut.service.commons.UploadFileManager;
import com.suitsoft.ut.service.select.SelectManager;
import com.suitsoft.ut.util.upload.XdbFiles;
import com.suitsoft.ut.web.CrudActionSupport;
import com.suitsoft.ut.web.InitListActionSupport;

/**
 * 查看上传答辩PPT
 *@author Suineg
 */
@Results({@Result(name=CrudActionSupport.RELOAD,location="upload-pptv-view",type="redirect")})
public class UploadPptViewAction extends InitListActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = -5422928322446972987L;
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	//Action's Result
	/** 查看 */
	private final String VIEW = "view";
	
	/** 学生管理  服务层对象  */
	private StudentManager studentManager;
	/** 选题 服务层对象 */
	private SelectManager selectManager;
	/** */
	private UploadFileManager uploadFileManager;
	
	/** 学生实体 */
	private Student student;
	/** 上传文件 */
	private File uploadFile;
	/** 上传文件名 */
	private String uploadFileFileName;
	/** 上传文件列表 */
	private List<XdbFiles> xdbFiles = Lists.newArrayList();
	/** 文件ID */
	private Long fid;
	/** 已上传文件集合 */
	private List<UploadFileInfo> uploadList = Lists.newArrayList();
	
	@Override
	public String execute() throws Exception {
		List<Thesis> list = selectManager.findSelectedForMiddle();
		Struts2Utils.getRequest().setAttribute("thesisList", list);
		return SUCCESS;
	}
	/**
	 * 查看
	 * @return
	 */
	public String view(){
		if(id == null || id == 0l){
			addActionMessage("非法操作");
		}
	//	UploaderDBUtil dbUtil = new UploaderDBUtil(ServletActionContext.getServletContext());
	//	xdbFiles = dbUtil.getFilesByUid(id.toString(),UploaderDBUtil.MIDDLE);
		thesis = thesisManager.get(id);
		uploadList = thesis.getMiddleFile();
		return VIEW;
	}
	
	public String download(){
		if(fid == null || fid == 0l){
			addActionMessage("非法操作");
			return SUCCESS;
		}
	//	UploaderDBUtil dbUtil = new UploaderDBUtil(ServletActionContext.getServletContext());
	//	XdbFiles files = dbUtil.getFileInfByFid(Integer.valueOf(fid.toString()));
		UploadFileInfo files = uploadFileManager.get(fid);
		try {
			Struts2Utils.getRequest().setAttribute("exportFilePath", files.getFilePathLocal());
			Struts2Utils.getRequest().setAttribute("exportFileName", files.getFileNameRemote());
			Struts2Utils.getRequest().getRequestDispatcher("/download.jsp").forward(Struts2Utils.getRequest(), Struts2Utils.getResponse());
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Thesis getThesis() {
		return thesis;
	}
	public void setThesis(Thesis thesis) {
		this.thesis = thesis;
	}
	public ThesisFile getThesisFile() {
		return thesisFile;
	}
	public void setThesisFile(ThesisFile thesisFile) {
		this.thesisFile = thesisFile;
	}
	public StudentManager getStudentManager() {
		return studentManager;
	}
	public void setStudentManager(StudentManager studentManager) {
		this.studentManager = studentManager;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}

	public File getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public String getUploadFileFileName() {
		return uploadFileFileName;
	}

	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}

	public List<XdbFiles> getXdbFiles() {
		return xdbFiles;
	}
	public void setXdbFiles(List<XdbFiles> xdbFiles) {
		this.xdbFiles = xdbFiles;
	}
	public Long getFid() {
		return fid;
	}
	public void setFid(Long fid) {
		this.fid = fid;
	}
	@Autowired
	public void setSelectManager(SelectManager selectManager) {
		this.selectManager = selectManager;
	}
	public List<UploadFileInfo> getUploadList() {
		return uploadList;
	}
	public void setUploadList(List<UploadFileInfo> uploadList) {
		this.uploadList = uploadList;
	}
	@Autowired
	public void setUploadFileManager(UploadFileManager uploadFileManager) {
		this.uploadFileManager = uploadFileManager;
	}
	
}

